package com.emenu;

import java.util.List;
import com.emenu.RightTopFragment.RightAdapter;
import com.emenu.application.StaticObject;
import com.emenu.bean.Menu;
import com.emenu.bean.StackBean;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import android.support.v4.app.FragmentTransaction;

public class FragmentMAin extends FragmentActivity {

	public TextView page_pathvalue;
	LeftFragment LeftFragment;
	RightTopFragment RightFragment;
	private RightAdapter mAdapter;
	private Intent intent;

	private Button button_have;
	private Button button_submit;
	public Button button_back;
	//已点菜单按钮点击后不需要加入路径
	int h=0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragmentmain);

		page_pathvalue = (TextView) findViewById((R.id.page_path));
		LeftFragment = (LeftFragment) getSupportFragmentManager()
				.findFragmentById(R.id.left);

		RightFragment = (RightTopFragment) getSupportFragmentManager()
				.findFragmentById(R.id.right);

		RightFragment.setMemberListener(new MemberListener() {
			@Override
			public void onMemberSelected(Menu menu) {
				LeftFragment.showImage(menu, 1);

			}
		});

		button_have = (Button) findViewById((R.id.have_menu));
		button_submit = (Button) findViewById((R.id.sure_post));
		button_back = (Button) findViewById((R.id.menu_cancel));

		setListener();

	}

	/** 设置监听器 */
	private void setListener() {
		button_have.setOnClickListener(haveListener);
		button_submit.setOnClickListener(submitListener);
		button_back.setOnClickListener(backListener);
	}

	private OnClickListener haveListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			h++;
			Fragment fg = getSupportFragmentManager().findFragmentById(
					R.id.left);
			fg.onPause();
			
			FragmentTransaction ft = getSupportFragmentManager()
					.beginTransaction();			 
			
			FragmentHaveHit HaveHit = new FragmentHaveHit();
			Bundle args = new Bundle();
			args.putInt("c1", -1);
			HaveHit.setArguments(args);

			ft.replace(R.id.left, HaveHit);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			ft.addToBackStack(null);

			ft.commit();
			page_pathvalue.setText("已点菜单");
		}
	};

	private OnClickListener submitListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			forward();
		}
	};

	private OnClickListener backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			if (!getSupportFragmentManager().popBackStackImmediate()) {
				getSupportFragmentManager().popBackStack();
			}

			Fragment ft = getSupportFragmentManager().findFragmentById(
					R.id.left);
			ft.onResume();
		
			if(h<1){
				StaticObject SO = (StaticObject) getApplication();
				if (SO.getStackList().size() > 1) {
					SO.getStackList().removeLast();
				}

				showpath();
			}
			
			h--;
			if(h<0){
				h=0;
				
			}
		
		}
	};

	public void showpath() {

		StaticObject SO = (StaticObject) getApplication();
		List<StackBean> sblist = SO.getStackList();

		StringBuffer bf = new StringBuffer("\\");

		for (StackBean sb : sblist) {
			bf.append(sb.getName() + "\\");

		}
		page_pathvalue.setText(bf.toString());

	}

	public void refreshRight() {
		mAdapter = (RightAdapter) RightFragment.getListAdapter();
		mAdapter.notifyDataSetChanged();

	}

	public void forward() {
		intent = new Intent();
		intent.setClass(FragmentMAin.this, SurePost.class);

		// 启动Activity
		startActivity(intent);

	}
}